'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class LinqToXmlGeneration
      Inherits MultiPass

#Region "Class level declarations"
      Private mGenerator As GenDotNet.LinqGenerator.Generator
      Private mAssemblyName As String
      Private mTemplateName As String
      Private mInitialized As Boolean
      Private mCategory As GenerateCategory
#End Region

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal statusDirective As IStatusDirective)
         Dim directive As CodeGenerationSupport.ILinqToXmlGenerationDirective
         Dim multiPassDirective As CodeGenerationSupport.IMultiPassDirective
         mGenerator = New GenDotNet.LinqGenerator.Generator()
         mInitialized = False

         directive = CType(node, CodeGenerationSupport.ILinqToXmlGenerationDirective)
         mAssemblyName = IO.Path.GetFullPath(directive.Process.AssemblyFileName)
         mTemplateName = directive.Process.TypeName
         mCategory = directive.Category

         multiPassDirective = CType(node, CodeGenerationSupport.IMultiPassDirective)
         Try
            Me.SetParams(directive.Process.Params)
            Me.MultiPass(multiPassDirective, _
                     Nothing, Nothing, _
                     AddressOf LinqToXmlGenerationCallBack, _
                     statusDirective, _
                     runUtility)
         Catch ex As System.Exception
            ' Exception should already be recorded
         End Try
      End Sub

#End Region

      '#Region "Protected properties and methods"
      '#End Region

#Region "Private properties and methods"
      Private Function LinqToXmlGenerationCallBack( _
               ByVal outputFileName As String, _
               ByVal multiPassDirective As CodeGenerationSupport.IMultiPassDirective, _
               ByVal nodeSelect As Xml.XmlNode, _
               ByVal runUtility As CodeGenerationSupport.RunUtility, _
               ByVal statusOutput As CodeGenerationSupport.IStatusOutput) _
               As IO.Stream
         If Not mInitialized Then
            Dim addInRoot As String = My.Settings.AddInRoot
            mGenerator.SetUpXmlDocument(nodeSelect.OwnerDocument.OuterXml(), addInRoot, Nothing)
            mInitialized = True
         End If
         Dim name As String = Utility.Tools.GetAttributeOrEmpty(nodeSelect, "Name")
         SetParameters(nodeSelect, outputFileName)
         Return mGenerator.GenerateStream(name, mAssemblyName, _
                     mTemplateName, mGenerator.StringToCategory(mCategory.ToString()))
      End Function
#End Region
   End Class
End Namespace